#include "StdAfx.h"
#include "Player.h"
#include "Chaser.h"
#include <ctime>

Chaser::Chaser(void) : Player(),
	nCurrentSteps(0),
	bMoveFoward(false),
	chaseMode(CHASE_MODE_RANDOM),
	searchRadius(0.0),
	moveDirection(0.0f, 0.0f, 0.0f),
	targetPos(0.0f, 0.0f, 0.0f)
{
	Load("data/Ferrari.x");
	camera.switchMode();
	srand((unsigned)time(0));
}

Chaser::~Chaser(void)
{
}

void Chaser::setDirection(float x, float y, float z)
{
	moveDirection.x = x;
	moveDirection.y = y;
	moveDirection.z = z;
}

void Chaser::setMode(ChaserMode mode)
{
	chaseMode = mode;
	nCurrentSteps = 0;
	nCurrentTurnSteps = 0;
}

void Chaser::update(D3DXVECTOR3 target, long time)
{
	float dirX = block->m_Pos.x - target.x;
	float dirZ = block->m_Pos.z - target.z;
	float magnitude = sqrt((dirX*dirX) + (dirZ*dirZ));
	if (magnitude < searchRadius)
	{
		targetPos.x = target.x;
		targetPos.y = target.y;
		targetPos.z = target.z;
		setMode(CHASE_MODE_TARGET);
	}
	else
	{
		setMode(CHASE_MODE_RANDOM); // move to last known location: targetPos
	}

	switch(chaseMode)
	{
		case CHASE_MODE_RANDOM:
			if (nCurrentSteps == 0)
			{
				nCurrentSteps = (rand() % 41) + 15; // max of 41 steps with a 15 step minimum
				bMoveFoward = !(nCurrentSteps % 20 == 1); // 1/40 prob that chaser will move backwards

				nCurrentTurnSteps = (rand() % 5) + 1; // max of 5 steps with a 1 step minimum
				bTurnLeft = (nCurrentTurnSteps % 2 == 1);

				// moving backwards? 4x turn step and 2x move step
				if (bMoveFoward == false)
				{
					nCurrentTurnSteps *= 4;
					nCurrentSteps *= 2;
				}

			}
			else
			{
				nCurrentSteps--;
			}

			// nCurrentTurnSteps should be much smaller than nCurrentSteps, thus it will be zero first
			// when it is zero stop turning and just move straight
			if (nCurrentTurnSteps > 0)
			{
				if (bTurnLeft) turnLeft();
				else turnRight();
				nCurrentTurnSteps--;
			}

			if (bMoveFoward) moveForward(false);
			else moveBackward(false);

			break;

		case CHASE_MODE_TARGET:

			// take current pos and target pos then get the vector needed to tarvel towards him
			if (targetPos != NULL)
			{
				float n1 = sqrt(block->m_Pos.x*block->m_Pos.x + block->m_Pos.z*block->m_Pos.z);
				float n2 = sqrt(targetPos.x*targetPos.x + targetPos.z*targetPos.z);
				m_fAngle = 1*acos((block->m_Pos.x*targetPos.x+block->m_Pos.z*targetPos.z)/(n1*n2)) * 180/3.1417;
				moveForward(false);
			}
			break;

		case CHASE_MODE_MOVE_TO:
			dirX = block->m_Pos.x - targetPos.x;
			dirZ = block->m_Pos.z - targetPos.z;
			magnitude = sqrt((dirX*dirX) + (dirZ*dirZ));

			moveDirection.x = (dirX / magnitude);
			moveDirection.z = (dirZ / magnitude);
			moveDirection = block->m_Pos - (moveDirection * m_fMovSpd);
			block->setCenterPosition(moveDirection);

			if (moveDirection.x > -0.2f && moveDirection.x < 0.2f)
			{
				moveDirection.x = 0.0f;
			}
			if (moveDirection.z > -0.2f && moveDirection.z < 0.2f)
			{
				moveDirection.z = 0.0f;
			}
			break;
	}
}



